<?php
// +----------------------------------------------------------------------
// | CLTPHP.Pro [后台管理员]
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2029 https://www.cltphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://www.cltphp.com/detail-140.html )
// +----------------------------------------------------------------------
// | Author: chichu <1109305987@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\model\Admin as M;
use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;
use think\facade\Request;

class Admin extends Base
{
    protected $validate = 'Admin';
    // 管理员列表
    public function index()
    {
        if (Request::isPost()) {
            //查出所有数据
            $list = M::get_list()->toArray();
            foreach ($list as $k=>$v){
                $list[$k]['logintime'] = date('Y-m-d H:i:s',$v['logintime']);
            }
            return ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
        }
        return view();
    }
    // 管理员添加
    public function add(){
        if(Request::isPost()){
            $data = input('post.');
            if (empty($data['group_id'])) {
                return ['code'=>1,'msg'=>'请选择用户组'];
            } else {
                $group_id = $data['group_id'];
                unset($data['group_id']);
            }
            //单独验证密码
            if (empty($data['pwd'])) {
                return ['code'=>1,'msg'=>'密码不能为空'];
            }
            //验证
            $result = $this->validate($data, $this->validate);
            if (true !== $result) {
                // 验证失败 输出错误信息
                return ['code'=>1,'msg'=>$result];
            }

            $data['mdemail'] =rand_string();
            $data['pwd']  = md5(trim($data['pwd']).$data['mdemail']);
            $data['logintime'] = time();
            $data['ip']   = Request::ip();
            //添加
            $result = M::create($data);
            if ($result) {
                AuthGroupAccess::create(['uid'=>$result->admin_id, 'group_id' =>  $group_id]);
                return ['code'=>0,'msg'=>'管理员添加成功'];
            } else {
                return ['code'=>1,'msg'=>'管理员添加失败'];
            }
        } else {
            $auth_group = AuthGroup::where('status',1)->select();
            $view = [
                'authGroup' => $auth_group,
                'info'      => null
            ];
            return view('form',$view);
        }
    }
    //编辑管理员
    public function edit(){
        if(request()->isPost()){
            $data = input('post.');
            $password = $data['pwd'];
            $group_id = $data['group_id'];
            unset($data['group_id']);
            $result = $this->validate($data,$this->validate);
            if (true !== $result) {
                // 验证失败 输出错误信息
                return ['code'=>1,'msg'=>$result];
            } else {
                $mdemail = M::where('admin_id',$data['admin_id'])->value('mdemail');
                if ($password){
                    $data['pwd']=md5(trim(input('post.pwd')).$mdemail);
                }else{
                    unset($data['pwd']);
                }
                if(session('admin.id') == $data['admin_id']){
                    session('admin.username',$data['username']);
                }
                $result = M::editSave($data);
                AuthGroupAccess::update(['group_id' =>  $group_id], ['uid'=>$data['admin_id']]);
                return $result;
            }
        }else{
            $auth_group = AuthGroup::select();
            $admin = M::getInfo(input('param.id'));
            $admin['group_id'] = AuthGroupAccess::where('uid', $admin['admin_id'])
                ->value('group_id');
            $view = [
                'info'      => $admin,
                'authGroup' => $auth_group,
            ];
            return view('form',$view);
        }
    }
    // 管理员删除
    public function del(){
        $admin_id = input('post.admin_id');
        if ($admin_id >1) {
            if (session('admin.id')==1){
                M::destroy($admin_id);
                AuthGroupAccess::where('uid',$admin_id)->delete();
                return ['code'=>0,'msg'=>'删除成功!'];
            }else{
                return ['code'=>1,'msg'=>'您没有删除管理员的权限!'];
            }
        } else {
            return ['code'=>1,'msg'=>'超级管理员不可删除'];
        }
    }
    // 管理员批量删除
    public function delAll(){
        $ids = input('post.ids/a');
        if (in_array('1',$ids)) {
            return ['code'=>1,'msg'=>'超级管理员不可删除'];
        }
        M::destroy($ids);
        $idStr = implode(',',$ids);
        AuthGroupAccess::where('uid','in',$idStr)->delete();
        return ['code'=>0,'msg'=>'删除成功!'];
    }
    //修改状态
    public function open(){
        $data = input('post.');
        if($data['admin_id']==1){
            return ['code'=>1,'msg'=>'超级管理员状态不可改变!'];
        }
        $result = M::editSave($data);
        return $result;
    }



}